package d3d9;

import static d3d9.D3D9.*;
import static d3d9.D3D9.Direct3DCreate9;
import static d3d9.D3D9.D3DFORMAT.*;
import static d3d9.D3D9.D3DSWAPEFFECT.*;
import static d3d9.D3D9.D3DDEVTYPE.*;
import static org.bridj.Pointer.*;
import static org.junit.Assert.assertNotNull;

import javax.swing.JFrame;

import org.bridj.Pointer;
import org.bridj.jawt.JAWTUtils;
import org.junit.Test;

import windows.HWND;

public class TestD3D9 {

	@Test
	public void testD3D9() {
		Pointer<IDirect3D9> pD3D9 = Direct3DCreate9(D3D_SDK_VERSION);
		assertNotNull(pD3D9);

		IDirect3D9 d3d9 = pD3D9.get();

		int adapterCount = d3d9.GetAdapterCount();
		System.out.println("Found " + adapterCount + " adapters");

		//D3DADAPTER_IDENTIFIER9 identifier = new D3DADAPTER_IDENTIFIER9();
		//d3d9.GetAdapterIdentifier(0, 0, pointerTo(identifier));
		
		D3DDISPLAYMODE mode = new D3DDISPLAYMODE();
		d3d9.GetAdapterDisplayMode(D3DADAPTER_DEFAULT, pointerTo(mode));
		
		
		D3DPRESENT_PARAMETERS d3dpp = new D3DPRESENT_PARAMETERS();
		d3dpp.Windowed(1);
		d3dpp.SwapEffect(D3DSWAPEFFECT_DISCARD);
		d3dpp.BackBufferFormat(D3DFMT_UNKNOWN);

		// Create JFrame
		JFrame frame = new JFrame("Test D3D11");
		frame.setSize(800, 600);
		frame.setIgnoreRepaint(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
		Pointer<Pointer<IDirect3DDevice9>> ppDevice = allocatePointer(IDirect3DDevice9.class);
		int hr = d3d9.CreateDevice( D3DADAPTER_DEFAULT, 
									D3DDEVTYPE_SW, 
									new HWND(JAWTUtils.getNativePeerHandle(frame)), 
									D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
									pointerTo(d3dpp), ppDevice );
		if(hr != 0) {
			System.out.println("Error creating device");
			return;
		}

		IDirect3DDevice9 device = ppDevice.get().getNativeObject(IDirect3DDevice9.class);
		
		device.Release();
		d3d9.Release();
	}
}
